package cn.xiaobai.captcha.enums;

import java.util.Calendar;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author yangdaji
 * @version 1.0
 * @Description: TimeUnit状态--> Calendar转换
 * @date 22-6-13 上午 1:07
 */
public enum UnitToCalendarUnitEnum {

    /**
     * 分钟
     */
    MINUTES(TimeUnit.MINUTES, Calendar.MINUTE),
    /**
     * 24时制小时
     */
    HOUR_OF_DAY(TimeUnit.HOURS, Calendar.HOUR_OF_DAY),
    /**
     * 天
     */
    DAY_OF_YEAR(TimeUnit.DAYS, Calendar.DAY_OF_YEAR),
    /**
     * 其他,用秒表示
     */
    OTHER(TimeUnit.SECONDS, Calendar.SECOND);

    private final TimeUnit timeUnit;
    private final int calendar;


    UnitToCalendarUnitEnum(TimeUnit timeUnit, int calendar) {
        this.timeUnit = timeUnit;
        this.calendar = calendar;
    }

    public TimeUnit getTimeUnit() {
        return timeUnit;
    }

    public int getCalendar() {
        return calendar;
    }

    public static int getCalendar(TimeUnit unit) {
        UnitToCalendarUnitEnum[] values = UnitToCalendarUnitEnum.values();
        for (UnitToCalendarUnitEnum value : values) {
            if (Objects.equals(value.getTimeUnit(), unit)) {
                return value.getCalendar();
            }
        }
        throw new IllegalStateException("没有匹配到相关类型");
    }
}
